package com.ifengchao.dao.impl;

import com.ibatis.sqlmap.client.SqlMapExecutor;
import com.ifengchao.dao.IBatisBaseDao1;
import com.ifengchao.dao.IBatisBaseDao3;
import com.ifengchao.dao.iface.DealPlanDao;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.ibatis.SqlMapClientCallback;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * @Author YiMing
 * @Description:
 * @Date: Created in 上午9:34 2017/9/1
 * @Modified By
 */

@Repository
public class DealPlanDaoImpl implements DealPlanDao {

	private static final Logger logger = Logger.getLogger(DealPlanDaoImpl.class);

	@Autowired(required=true)
	private IBatisBaseDao1 iBatisBaseDao1;

	@Autowired(required=true)
	private IBatisBaseDao3 iBatisBaseDao3;

	@Override
	public List<Map<String, Object>> queryZzyAdvertiseId() {
		logger.info("查询所有智子云广告主id");
		return iBatisBaseDao3.sqlMapClientTemplate.queryForList("dealPlanSql.queryZzyAdvertiseId");
	}

	@Override
	public void insertZzyStatisticsDay(List<Map<String, Object>> reportData, final Object id, final Object sar) {
		logger.info("更新智子云效果统计");
		final List<Map<String, Object>> tempList = reportData;
		try {
			if ( tempList != null) {
				iBatisBaseDao1.getSqlMapClientTemplate().execute(
						new SqlMapClientCallback() {
							public Object doInSqlMapClient(SqlMapExecutor executor) throws
									SQLException {
								executor.startBatch();
								for (int i = 0, n = tempList.size(); i < n; i++) {
									Map<String, Object> map = tempList.get(i);
									map.put("userIds",id);
									map.put("sar",sar);
									/*BigDecimal totalCost = (BigDecimal) map.get("totalCost");
									totalCost.multiply(new BigDecimal(Integer.parseInt(sar.toString())));
									map.put("totalCost",totalCost);*/
									executor.update("dealPlanSql.insertZzyStatisticsDay",map);
								}
								executor.executeBatch();
								return null;
							}
						}
				);
			}
		}catch (Exception e){
			e.printStackTrace();
		}

	}
}
